package com.usian.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.article.feign.BehaviorFeign;
import com.usian.article.mapper.ApCollectionMapper;
import com.usian.article.service.ApArticleService;
import com.usian.article.service.ApCollectionService;
import com.usian.common.constants.message.HotArticleConstants;
import com.usian.model.article.dtos.CollectionBehaviorDto;
import com.usian.model.article.pojos.ApArticle;
import com.usian.model.article.pojos.ApCollection;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.mess.app.UpdateArticleMess;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service
public class ApCollectionServiceImpl extends ServiceImpl<ApCollectionMapper, ApCollection> implements ApCollectionService {

    @Autowired
    private BehaviorFeign behaviorFeign;

    @Autowired
    private ApArticleService apArticleService;

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Override
    public ResponseResult saveCollection(CollectionBehaviorDto dto) {

        //1.检查参数
        if(dto == null || dto.getEntryId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.查询行为实体
        ApUser user = AppThreadLocalUtils.getUser();
        ApBehaviorEntry apBehaviorEntry = behaviorFeign.findByUserIdOrEntryId(user.getId(), dto.getEquipmentId());
        if(apBehaviorEntry == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //发送消息
        if(dto.getOperation() == 0){
            UpdateArticleMess mess = new UpdateArticleMess();
            mess.setAdd(1);
            mess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
            mess.setArticleId(dto.getEntryId());
            kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC, UUID.randomUUID().toString(), JSON.toJSONString(mess));
        }

        //3.收藏或取消收藏
        ApCollection apCollection = getOne(Wrappers.<ApCollection>lambdaQuery().eq(ApCollection::getArticleId, dto.getEntryId()));
        ApArticle apArticle = apArticleService.getById(dto.getEntryId());
        if(apCollection == null && dto.getOperation() == 0){
            apCollection = new ApCollection();
            apCollection.setType(dto.getType());
            apCollection.setArticleId(dto.getEntryId());
            apCollection.setEntryId(apBehaviorEntry.getId());
            apCollection.setCollectionTime(new Date());
            apCollection.setPublishedTime(apArticle.getPublishTime());
            save(apCollection);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else {
            removeById(apCollection);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
    }
}
